package cn.edu.tit.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Login {
    public static void main(String[] args) {
    	Scanner scanner = new Scanner(System.in);
    	System.out.println("请输入用户名：");
    	String uname = scanner.next();
    	
    	System.out.println("请输入密码：");
    	String upsw = scanner.next();
    	
    	try {
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://localhost:3306/titdb";
			Connection connection = DriverManager.getConnection(url,"root","root");
			String sql = "SELECT * FROM users WHERE username=? AND userpsw=?";
			PreparedStatement pstmt = connection.prepareStatement(sql);
			pstmt.setString(1, uname);
			pstmt.setString(2, upsw);
			
			ResultSet rs = pstmt.executeQuery();
			if (rs.next()) {
				System.out.println("登陆成功！");
			} else {
				System.out.println("登陆失败！");
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}
